% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

clear all
if ~isfolder('Results')
    mkdir('.','Results');
end
load('../data_array_for_regression_stacked_by_variable_CDS_new.mat');
BP_shock_base=100*squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.BP_shock_base));
% BP_shock_base(:,24)=NaN;
FC_errors.country_data.VAR=BP_shock_base;
FC_errors.stacked_data.VAR=BP_shock_base(:);
OE_g_growth_fe=100*squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.g_growth_fe));
FC_errors.country_data.OE=OE_g_growth_fe;
FC_errors.stacked_data.OE=OE_g_growth_fe(:);
OECD=load('../data_array_for_regression_stacked_by_variable_semi_annual_CDS');
OECD_g_growth_fe=100*squeeze(OECD.data_array_for_regression_stacked_by_variable(:,:,OECD.pos.OECD_Forecast_Error_g));
FC_errors.country_data.OECD=OECD_g_growth_fe;
FC_errors.stacked_data.OECD=OECD_g_growth_fe(:);

FE_string={'OE','OECD','VAR'};
linestring={'k-','k--','k-.'};
n_cases=size(FE_string,2);
n_countries=size(FC_errors.country_data.(FE_string{1}),2);
h=NaN(n_countries,n_cases);
h_bias=NaN(n_countries,n_cases);
pValue=NaN(n_countries,n_cases);
fig_handle=figure;
subplot(2,1,1)
for FC_iter=1:n_cases
    stats(2,FC_iter)=sum(~isnan(FC_errors.stacked_data.(FE_string{FC_iter})));
    stats(3,FC_iter)=nanmean(FC_errors.stacked_data.(FE_string{FC_iter}));
    stats(4,FC_iter)=min(FC_errors.stacked_data.(FE_string{FC_iter}));
    stats(5,FC_iter)=max(FC_errors.stacked_data.(FE_string{FC_iter}));
    stats(6,FC_iter)=sqrt(nanmean(FC_errors.stacked_data.(FE_string{FC_iter}).^2));
    acf=NaN(n_countries,9);
    country_counter=0;
    for country_iter=1:n_countries
        if ~all(isnan(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter)))
            [acf(country_iter,:),lags,bounds]=autocorr(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter),'NumLags',8);
            [b,bint,r,rint,stats_reg]=regress(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter),[ones(size(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter)))]);
            if bint(1)>0 || bint(2)<0
                h_bias(country_iter,FC_iter)=1;
            else
                h_bias(country_iter,FC_iter)=0;
            end
            [h(country_iter,FC_iter),pValue(country_iter,FC_iter)] = lbqtest(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter),'lags',4);
            if h(country_iter,FC_iter)
                figure('Name',[country_header{country_iter},' ', FE_string{FC_iter}])
                autocorr(FC_errors.country_data.(FE_string{FC_iter})(:,country_iter),'NumLags',8);
            end
            country_counter=country_counter+1;
        end
    end
    stats(1,FC_iter)=country_counter;
    stats(8,FC_iter)=nansum(h(:,FC_iter));
    stats(7,FC_iter)=nansum(h_bias(:,FC_iter));
    [f,xi] = ksdensity(FC_errors.stacked_data.(FE_string{FC_iter}));
    figure(fig_handle)
    plot(xi,f,linestring{FC_iter},'LineWidth',1.5)
    hold on
end
legend(FE_string)
xlim([-8 8])
xlabel('Forecast error (pps)')
print('-depsc2','Results/FE_kernel')
labels=char('Countries','T','Mean','Min','Max','RMSE','Sign. constants','LBQ rejections');
make_latex_table('FC','Forecast Errors: Descriptive Statistics','stats',char([' ',FE_string]),labels,stats,10,8,7)